package com.sys.core.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Str2MD5 {

    public static void main(String[] args) {
        MD5("appid=wxcbaf0aec64b1b07d&body=测试1商品&mch_id=1366381802&nonce_str=bYbGBIVji4IdChAHBYr&notify_url=http%3a%2f%2fwww.51d1d.com%2fchezhujie%2fadmin%2fapiTest.do&openid=ood9BvxGOR2BJKZN2Hg3x0ej2Cjw&out_trade_no=1234567890&spbill_create_ip=127.0.0.1&time_expire=20170610160301&time_start=20170610150301&total_fee=1&trade_type=JSAPI&key=dfUtjlsyjf39j1j3n58sd0g08f7d5jf6");
    }

    private static String MD5(String sourceStr) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
            System.out.println("MD5(" + sourceStr + ",32) = " + result);
            System.out.println("MD5(" + sourceStr + ",16) = " + buf.toString().substring(8, 24));
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e);
        }
        return result;
    }
}